import {Component, OnInit,TemplateRef, OnDestroy} from '@angular/core';
import {NormalService} from "../../../apiServices/normal.service";
import {Subscription} from 'rxjs/Subscription';
import {Router, ActivatedRoute} from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';


@Component({
    selector: 'changepwd',
    templateUrl: './changepwd.template.html'
})

export class ChangepwdComponent implements OnInit, OnDestroy{
    public language: string = "en";
    public changePass = {
        password: '',
        repassword: '',
        changecode: '',
        email: ''
    }
    isPwdvalid: boolean = true;

    constructor(
        private normalService: NormalService,
        private activated: ActivatedRoute,
        private router: Router,
    ) {
        this.language = normalService.language;
        this.activated.params.subscribe(param => {
            console.log('param',param);
            this.changePass.changecode = param.code;
            this.changePass.email = param.email;
        })
    }
    

    isPwdsame(){
        if (this.changePass.password != this.changePass.repassword){
            this.isPwdvalid = false;
            console.log("passValid:" + this.isPwdvalid);
        } else{
            this.isPwdvalid = true;
            console.log("passValid:" + this.isPwdvalid);
        }
    }

    submitPassword(password: string): void{
        this.normalService.submitPassword(this.changePass.email, password, this.changePass.changecode).then((result) => {
                alert(this.normalService.getSucc(result.message));
                this.router.navigate(['/']);
        }).catch((error) =>{
            alert(this.normalService.getErrors(error));
        });
    }

    ngOnInit(){
    }
    ngOnDestroy(){}
}
